package com.cookbook.controller;

import com.cookbook.RequestAuthority;
import com.cookbook.Result;
import com.cookbook.database.model.Cookbook;
import com.cookbook.database.model.User;
import com.cookbook.service.CookbookService;
import com.cookbook.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 胡玉斌 on 2017/7/3.
 */
@Controller
@RequestMapping("/cookbook")
public class CookbookController {
    @Autowired
    CookbookService cookbookService;
    @Autowired
    UserService userService;

    @Transactional
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public String list(
            @RequestParam(name="page") int page,
            @RequestParam("limit") int limit) {
        if (page < 0 || limit < 0) {
            return Result.format(Result.PARAMETER_INVALID, "非法参数");
        }
        List<Cookbook> cookbooks;
        try {
            cookbooks = cookbookService.select(page, limit);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
        return Result.success(cookbooks);
    }

    @Transactional
    @RequestMapping(value="/insert", method = RequestMethod.POST)
    @ResponseBody
    public String insert(
            @RequestParam("title") String title,
            @RequestParam("content") String content,
            @RequestParam("cover") String coverUrl) {
        if (title.length() > 64 || title.isEmpty() ||
                content.length() > 255 || content.isEmpty() ||
                coverUrl.length() > 255 || coverUrl.isEmpty())
            return Result.format(Result.PARAMETER_INVALID, "非法参数");

        try {
            User user = userService.selectWithId(1);
            Cookbook cookbooks = new Cookbook();
            cookbooks.setTitle(title);
            cookbooks.setContent(content);
            cookbooks.setCover(coverUrl);
            cookbooks.setUser(user);
            cookbookService.insert(cookbooks);
            return Result.success(cookbooks);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
    }
    @Transactional
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public String search(
            @RequestParam(name="search_cookbook") String search_cookbook, HttpServletRequest request) {
        List<Cookbook> cookbooks;
        String name = request.getLocalName();
        try {
            cookbooks = cookbookService.search(search_cookbook);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
        return Result.success(cookbooks);
    }
}
